﻿//$(function() {
//    //加入购物车效果-门店电脑配置不高有卡顿已去掉(2016-12-2)
//    //fly();
//    ComputedPayHeight();
//});
var shop_api_key = $(".shop_api_key").val();
//加入购物车
function addCart(productId,total,price) {
    //透明遮罩层
    layer.load(0);
    $(".layui-layer").hide();
    var cartQty = $(".circular_" + productId+":eq(0)").text();
    $(".circular_" + productId).text(parseFloat(cartQty) + total);
    $(".circular_" + productId).show();
    //加入购物车
    $.ajax({
        url: configs.s_url+"pcorder/index/addCart",
        type: "post",
        data: { "m_id": productId ,"total":total,"price":price},
        success: function (json) {
            var data = $.parseJSON(json);
            layer.closeAll();
            if (data.code == "200") {
                layer.msg('成功加入购物车', { time: 1000 });
                $("#cartCount").html(data.total);
                clearCartCookie();
                selectCart();
            } else {
                layer.alert(data.msg);
            }
        }
    });
}


function selectCart() {

    //加入购物车
    $.ajax({
        url: configs.s_url+"pcorder/index/seclectCart",
        type: "post",
        data: { },
        success: function (json) {
            var data = $.parseJSON(json);$(".hcartlist").html('');

            if (data.code == "200") {
                var html='';
                html+=' <li><span>名称</span><span>单价</span><span>数量</span></li>';
                if(data.datas.length>0){
                    for(var i=0;i<data.datas.length;i++){
                        html+='<li><span>'+data.datas[i].m_name+'</span><span>'+data.datas[i].price+'</span><span>'+data.datas[i].total+'</span></li>';
                    }
                }
                $(".hcartlist").append(html);
            }
        }
    });
}
//加入收藏
function addCollect(c_id,nums) {
    layer.load(0);
    $.ajax({
        url: configs.s_url+"pcorder/index/addCollect",
        type: "post",
        data: { "c_id": c_id ,"nums":nums},
        success: function (json) {
            var data = $.parseJSON(json);
            layer.closeAll();
            if (data.code == "200") {
                layer.msg('收藏成功', { time: 1000 });
            } else {
                layer.alert(data.msg);
            }
        }
    });
}


//加入购物车效果
function fly() {
    $(".buy").click(function (event) {
        var addcar = $(this);
        var img = addcar.parents("li").find('img').attr('src');
        var flyer = $('<img class="u-flyer" src="' + img + '">');
        flyer.fly({
            start: {
                left: event.pageX - 200,
                top: event.pageY - $(document).scrollTop()-250
            },
            end: {
                left: $(".cart").offset().left + 60,
                top: $(".cart").offset().top + 20 - $(document).scrollTop(),
                width: 0,
                height: 0
            },
            onEnd: function () {
                //alert("成功");
            }
        });
    });
}

//第一次弹窗计算小于五条的时候高度是可变的
function ComputedPayHeight() {
    if ($(".ordingContent li").length < 5) {

        $(".pay_mainBox").css("height", "auto");
        var h = $(".ordingContent ul").height() + 20;
        $(".ordingContent").css("height", h);
        // $(".ordingContent").css("overflow-y", hidden);
        $(".pay_mainBox").css("paddingBottom", "20px");
        $(".pay_mainBox").css("marginTop", -($(".pay_mainBox").height() / 2));
    };
}
var configs = {
    s_url:$.trim($(".shop_site_path").val()),
    api_key:shop_api_key,
    api_sgin:"2fc630bc826b90438579c2e6f5552217",
    user_id:$.trim($(".shop_user_id").val())||0
}
var indexHttpHelper = {
    s_url:configs.s_url,
    params:'json={"api_key":"'+configs.api_key+'","user_id":'+configs.user_id+',"api_sgin":"'+configs.api_sgin+'","client_type":2,"ver":13,',
    getAdvertList:function(currentObj,fileName,hiddenUrl,hidden_img){
        var params = indexHttpHelper.params+'"m":"t_26_guanggaoliebiao","map":[["id","gt","0"],["pos","eq","1"]],"ord":["create_time desc"],"service":"Grain.advert","p":0,"page_size":20,"task_id":"home_advert_list"}';
        this.doPost(params,currentObj,fileName,hiddenUrl,hidden_img);
    },
    getFirstKind:function(currentObj,fileName,hiddenUrl){
        var params = indexHttpHelper.params+'"m":"t_26_product_kind","ord":["order_id asc"],"map":[["parent_id","eq","0"]],"product_size":"6","service":"Grain.select","page_size":2000,"task_id":"home_frist"}';
        this.doPost(params,currentObj,fileName,hiddenUrl);

    },
    getHotGoods:function(currentObj,fileName,hiddenUrl){
        var params = indexHttpHelper.params+'"m":"t_26_menu","ord":["order_id asc"],"map":[["m_tag","eq","1"]],"service":"Grain.select","page_size":100,"task_id":"home_hot_goods"}';
        this.doPost(params,currentObj,fileName,hiddenUrl);

    },
    doPost:function(params,currentObj,fileName,hiddenUrl,hidden_img){
        var result = {};
        $.ajax({
            type:"post",
            url:this.s_url,
            dataType:'json',
            data:params,
            beforeSend:function(){
                Loading =layer.msg('努力加载中...', {icon: 16,shade: [0.5, '#f5f5f5'],scrollbar: false,offset: '40%', time:100000}) ;

            },
            success:function Response(data){
                layer.close(Loading);
                indexHttpHelper.callback(data,currentObj,fileName,hiddenUrl,hidden_img);
            }
        });
        return result;
    },
    callback:function(obj,currentObj,fileName,hiddenUrl,hidden_img){
        switch (obj.task_id){
            case 'home_advert_list':
                if(obj.code==200) {
                    if(obj.list.length>0){
                        var html='';
                        for( var i=0;i<obj.list.length;i++){
                            html+=' <div class="swiper-slide">';
                            if(obj.list[i].type==2){
                                html+='<a href="'+hiddenUrl+obj.list[i].id+'">';
                                html+='<img src="'+fileName+obj.list[i].image+'" onerror="this.src="'+hidden_img+'" " original="'+hidden_img+'"/>';
                                html+=  '</a>';
                            }else{
                                html += '<a href="'+obj.list[i].url+'">';
                                html+='<img src="'+fileName+obj.list[i].image+'"/>';
                                html+=  '</a>';
                            }

                            html+=  '</div>';
                        }
                        currentObj.append(html);
                    }
                    var mySwiper = new Swiper('.h-swiper-container', {
                        pagination : '.swiper-pagination',
                        autoplay: 3000
                    })
                }else{
                    alert('error');
                }
                break;
            case 'home_frist':
                if(obj.code==200) {
                    var html='';
                    if(obj.list.length>0){
                        for( var i=0;i<obj.list.length;i++){
                            if(obj.list[i].child_list.length>0){
                            html+='<li class="hli">'
                            html+='<i>'+obj.list[i].kind_name+'&gt;'
                            html+='</i>'
                            html+='<div class="allShop-box"><div class="allShop-box2">'
                            html+='<div class="allShop-name">'

                                //第二级分类
                                for( var j=0;j<obj.list[i].child_list.length;j++){
                                    html += '<div class="clearfix">'
                                    html += '<div class="main"><a href="#">'+obj.list[i].child_list[j].kind_name+'</a></div>'
                                    html+=   '<div class="classify">'
                                    //第三级分类
                                    for( var m=0;m<obj.list[i].child_list[j].child_list.length;m++) {
                                        html += ' <a href="' + hiddenUrl + obj.list[i].child_list[j].child_list[m].kind_id + '">' + obj.list[i].child_list[j].child_list[m].kind_name + '</a>'
                                       if(m != obj.list[i].child_list[j].child_list.length-1){
                                           html += '  <span>&nbsp;|&nbsp;</span>'
                                       }
                                    }
                                    html+='</div>'
                                    html+='</div>'
                                }

                            html+=   '</div></div></div>'
                            html+=' </li>'
                            }
                        }

                    }
                    currentObj.append(html);
                    $("li.hli").hover(function(){
                        $(this).addClass("li1");
                        $(this).siblings().removeClass("li1");
                        var idx=$(this).index()+1
                        $(this).find(".allShop-box").css("top","-"+(idx*46))
                        $(this).find(".allShop-box").show();
                        $(this).siblings().find(".allShop-box").hide();
                        },function(){
                        $(this).removeClass("li1");
                        $(".allShop-box").hide();
                        });

                }else{
                    alert('error');
                }
                break;
            case 'home_hot_goods':
                if(obj.code==200) {
                    var html='';
                    if(obj.list.length>0){
                        for( var i=0;i<obj.list.length;i++){
                            html += '<li>'
                            html += ' <div class="picture">'
                            html += '<div class="cu_big_pic"></div>'
                            html += '<a href="detail.html">'
                            html += '<img src="http://file.51guipiao.com/2013/'+obj.list[i].attach[0].file_name+'" onerror="" original="">'
                            html += ' </a>'
                            html += '<h2>零售价：'+obj.list[i].m_usual_price+' 元/瓶</h2>'
                            html += '<h2>批发价：'+obj.list[i].m_price+' 元/瓶&nbsp;&nbsp;&nbsp;</h2>'
                            html += '<h1 title="洋河蓝色经典480ml52°海之蓝（商）">'
                            html += ' <p><a href="http://b2b.frxs.com/Product/Detail?productId=2275">'+obj.list[i].m_name+'</a></p>'
                            html += '<p class="red" title="'+obj.list[i].m_desc+'">'+obj.list[i].m_desc+'</p>'
                            html += '</h1>'
                            html += '<div class="bottom">'
                            html += '   <div class="erp">商品编码：'+obj.list[i].m_id+'</div>'
                            html += '<div class="ording" onclick="addCart('+obj.list[i].m_id+')">'
                            html += '   <a href="javascript:void(0)" id="btnBuy_'+obj.list[i].m_id+'" class="buy"></a>'
                            html += '   <span class="circular circular_'+obj.list[i].m_id+'" style="display: none">0</span>'
                            html += '  </div>'
                            html += '   </div>'
                            html += '   </div>'
                            html += '   </li>';
                        }

                    }
                    currentObj.append(html);
                }else{
                    alert('error');
                }
                break;
            default :
                alert('error');
                break;
        }
    }
}
